<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>评论列表</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.css">
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h2>评论列表</h2>
                    </div>
                    <div class="panel-body">
                        <ul class="list-group comment-list">

                        </ul>
                    </div>
                    <div class="panel-footer">
                        <form onsubmit="addComment(event)">
                            <div class="form-group">
                                <label for="username">用户名</label>
                                <input type="text" id="username" class="form-control" placeholder="用户名"/>
                            </div>
                            <div class="form-group">
                                <label for="content">内容</label>
                                <input type="text" id="content" class="form-control" placeholder="内容"/>
                            </div>
                            <div class="form-group">
                                <input type="submit" class="btn btn-primary">
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
    <script>
        function getComments(){
            $.get('/api/comments').then(comments=>{
                let html = comments.map(comment=>(
                    `
                      <li class="list-group-item">
                        <div class="media">
                            <div class="media-left">
                                <a href="#">
                                    <img style="border-radius:5px;" class="media-object" src="${comment.avatar}"/>
                                </a>    
                            </div>
                            <div class="media-body">
                                <h4 class="media-heading">用户名: ${comment.username}</h4>
                                <p>内容: ${comment.content}</p>
                                <p>时间: ${comment.createAt}</p>
                            </div>
                        </div>
                      </li>
                    `
                )).join('');
                $('.comment-list').html(html);
            });
        }
        getComments();
        function addComment(event){
            event.preventDefault();//阻止默认事件
            let username = $('#username').val();
            let content = $('#content').val();
            $.post('/api/comments',{username,content}).then(function(){
                getComments();
            });
        }
    </script>
</body>
</html>